limsOperationBatch.showListPage = {

    getOperationBatchGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: 'lims-operation-batch-show-list-grid',
            url: limsOperationBatch.baseUrl + '/queries',
            requestData: {
                createdByOrgId_EQ: workspace.user.ext$.loginorgid
            },
            page: true,
            columns: limsOperationBatch.getOperationBatchGridColumns(),
            onCellClick: function (field, row) {
            },
            filterOpen: true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.loadInfoList(null)
                }
            },
            onRowActive: function (index, row) {
                _this.loadInfoList(row)
            },
            toolbar: []
        }
    },

    loadInfoList: function (row) {
        var _this = this;
        // 下发批次详情信息
        Gikam.getAsyncComp('lims-operation-batch-sample-task-list-grid').done(function (grid) {
            if (row) {
                grid.setOptions({
                    url: limsOperationBatch.preFolderUrl + '/issue/sample/queries',
                    requestData: {
                        operationBatch_EQ: row.operationBatch,
                        // status_EQ: 'draft',
                        addInBatch_EQ: '1'
                    }
                });
                _this.refreshGridColumns(grid, row.operationBatch);
                grid.refresh();
            } else {
                grid.cleanData();
                grid.options.columns = limsOperationBatch.getBatchSampleTaskGridColumns();
                grid.refreshColumns(limsOperationBatch.getBatchSampleTaskGridColumns());
            }
        });
    },

    refreshGridColumns: function (grid, operationBatch) {
        var _this = this;
        var url = Gikam.printf(limsOperationBatch.preTaskSupplementUrl + '/{operationBatch}/supplement-to-columns/queries', {
            operationBatch: operationBatch
        });
        limsOperationBatch.supplementColumns = limsOperationBatch.getBatchSampleTaskGridColumns();
        Gikam.postSync(url).done(function (data) {
            if (Gikam.isNotEmpty(data)) {
                Gikam.each(data.rows, function (index, item) {
                    var fieldName = item.fieldName;
                    limsOperationBatch.supplementColumns.push({
                        field: 'ext$.' + fieldName,
                        title: item.unit ? fieldName + '(' + item.unit + ')' : fieldName,
                        validators: (item.requiredFlag === '1' || item.requiredflag === '1') ? ['notEmpty'] : null,
                        // editor: true,
                        width: 120,
                        sort: false,
                        autoSave: false,
                        onChange: function (field, value, index) {
                        },
                        onBeforeEditorRender: function (row) {
                            return row.ext$.supplementinfo && eval('(' + row.ext$.supplementinfo + ')').hasOwnProperty(fieldName);
                        },
                        styleFormatter: function (row) {
                            return {
                                // backgroundColor: (row.ext$.supplementinfo && eval('(' + row.ext$.supplementinfo + ')')[fieldName])? '#7CFC00': null
                            }
                        }
                    });
                });
            }
        });
        grid.cleanData();
        grid.options.columns = limsOperationBatch.supplementColumns;
        grid.refreshColumns(limsOperationBatch.supplementColumns);
    },

    getBatchSampleTaskGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: 'lims-operation-batch-sample-task-list-grid',
            // url: limsOperationBatch.baseUrl + '/queries/rows',
            // requestData: requestData,
            page: true,
            columns: limsOperationBatch.getBatchSampleTaskGridColumns(),
            onCellClick: function (field, row) {
                field === 'batchCode' && row.batchId && Gikam.loadBatchDetailPageV12(row.batchId, true)
                field === 'sampleCode' && row.batchSampleId && _this.openBatchSampleTaskDetail(row)
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                }
            },
            onRowActive: function (index, row) {
            },
            toolbar: []
        }
    },

    openBatchSampleTaskDetail: function (row) {
        workspace.window.load(limsOperationBatch.odTaskUrl + '/page/sample-test-task-detail' + Gikam.param({
            batchSampleId: row.batchSampleId,
        }))
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-operation-batch-show-list-layout',
            renderTo: workspace.window.$dom,
            west: {
                width: '35%',
                // title: 'T_LIMS_OPERATION_BATCH.TITLE.RUN',
                items: [_this.getOperationBatchGrid()]
            },
            center: {
                items: [this.getBatchSampleTaskGrid()]
            }
        })
    },

    init: function () {
        this.create()
    }
}

